<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // 创建文件表
        Schema::create('files', function (Blueprint $table) {
            $table->id();
            $table->string("file_name", 225)->comment("文件名");
            $table->longText("file_path")->comment("文件地址");
            $table->string("file_type", 225)->comment("文件类型");
            $table->bigInteger('file_size')->comment("文件大小(Byte-字节)");
            $table->enum("file_state", ['ON', 'OFF'])->default('ON')->comment("文件是否有效(on为有效,off为无效)");
            $table->bigInteger('file_parent_folder_id')->comment('文件对应的父文件夹id');
            $table->string("file_alias", 225)->comment("别名");
            $table->integer("file_image_id")->comment("文件图标id");
            // 可以处理单个文件权限
            $table->integer("file_mod")->comment("文件权限");
            $table->integer("user_id")->comment("拥有者id");
            $table->integer("group_id")->comment("属组id");
            $table->timestamps();
        });
        // 创建文件夹表
        Schema::create('folders', function (Blueprint $table) {
            $table->bigIncrements('folder_id')->comment('文件夹id');
            $table->string('folder_name', 255)->comment('文件夹名称');
            $table->bigInteger('id')->unsigned()->comment('用户表中对应的用户id');
            $table->bigInteger('storage_space')->nullable()->comment('个人存储空间大小默认3GB(换算Byte-字节=3221225472)');
            $table->bigInteger('parent_folder_id')->unsigned()->nullable()->comment('父级文件夹id(0为用户根目录)');
            $table->enum("folder_state", ['ON', 'OFF'])->default('ON')->comment("文件夹是否有效(on为有效,off为无效)");
            $table->integer('folder_image_id')->default('519')->comment('文件夹图标id');
            $table->timestamps();


            // 设置外键约束
            $table->foreign('id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('parent_folder_id')->references('folder_id')->on('folders');
        });
        Schema::create('file_to_tag', function (Blueprint $table) {
            $table->id();
            $table->integer("file_id")->comment("文件id");
            $table->integer("tag_id")->comment("标签id");
        });
        Schema::create('tag', function (Blueprint $table) {
            $table->id();
            $table->string("tag_name")->comment("标签名");
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('files');
        Schema::dropIfExists('folders');
        Schema::dropIfExists('file_to_tag');
        Schema::dropIfExists('tag');
    }
};
